﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
/*
 http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=2&num=40&sort=changepercent&asc=0&node=sh_a&_s_r_a=page
 */
namespace QuoteDAL
{
    public class StockDAL
    {
        /// <summary>
        /// 获取沪市A股
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public string GetStockSHA(int page, int size = 40)
        {
            string url = " http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=" + page + "&num=" + size + "&sort=changepercent&asc=0&node=sh_a&_s_r_a=page";
            string content = HttpDAL.ReadUrlContent(url, "gb2312");
            return content;
        }

        /// <summary>
        /// 获取深圳A股
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public string GetStockSZA(int page, int size = 40)
        {
            string url = " http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=" + page + "&num=" + size + "&sort=changepercent&asc=0&node=sz_a&_s_r_a=page";
            string content = HttpDAL.ReadUrlContent(url, "gb2312");
            return content;
        }
        /// <summary>
        /// 从网络上面获取所有股票
        /// </summary>
        public void InitStock()
        {
            for (int i = 1; i < 24; i++)
            {
                DB.DBManager db = new DB.DBManager();
                string content = GetStockSHA(i);
                string[] contents = content.Split(new char[] { '}' });
                for (int k = 0; k < contents.Length; k++)
                {
                    string codePattern = @"code:\""[\s\S]*?(?<code>\S+)\"",name:";
                    string namePattern = @"name:\""[\s\S]*?(?<name>\S+)\"",trade:";
                    Regex codeReg = new Regex(codePattern);
                    Regex nameReg = new Regex(namePattern);

                    MatchCollection names = nameReg.Matches(contents[k]);
                    MatchCollection codes = codeReg.Matches(contents[k]);

                    for (int j = 0; j < names.Count; j++)
                    {
                        Match nameMatch = names[j];
                        string name = nameMatch.Groups["name"].ToString().Trim();
                        Match codeMatch = codes[j];
                        string code = codeMatch.Groups["code"].ToString().Trim();
                        Quote.Stock stock = new Quote.Stock();
                        stock.Code = code.Split(new char[] { '"' })[0];
                        stock.Name = name.Split(new char[] { '"' })[0];
                        stock.Market = "sh";
                        db.Stocks.Add(stock);
                    }
                }
                //code:"600059",name:"古越龙山"

                db.SaveChanges();
                db.Dispose();
            }


            for (int i = 1; i < 40; i++)
            {
                DB.DBManager db = new DB.DBManager();
                string content = GetStockSZA(i);
                string[] contents = content.Split(new char[] { '}' });
                for (int k = 0; k < contents.Length; k++)
                {
                    string codePattern = @"code:\""[\s\S]*?(?<code>\S+)\"",name:";
                    string namePattern = @"name:\""[\s\S]*?(?<name>\S+)\"",trade:";
                    Regex codeReg = new Regex(codePattern);
                    Regex nameReg = new Regex(namePattern);

                    MatchCollection names = nameReg.Matches(contents[k]);
                    MatchCollection codes = codeReg.Matches(contents[k]);

                    for (int j = 0; j < names.Count; j++)
                    {
                        Match nameMatch = names[j];
                        string name = nameMatch.Groups["name"].ToString().Trim();
                        Match codeMatch = codes[j];
                        string code = codeMatch.Groups["code"].ToString().Trim();
                        Quote.Stock stock = new Quote.Stock();
                        stock.Code = code.Split(new char[] { '"' })[0];
                        stock.Name = name.Split(new char[] { '"' })[0];
                        stock.Market = "sz";
                        db.Stocks.Add(stock);
                    }
                }
                //code:"600059",name:"古越龙山"

                db.SaveChanges();
                db.Dispose();
            }
        }
    }
}
